package com.jxtc.enterprise.delivery.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.delivery.dto.login.RiderLoginDTO;
import com.jxtc.enterprise.delivery.service.RiderLoginService;
import com.jxtc.enterprise.delivery.vo.login.RiderLoginSuccessVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 配送员 H5 "登录" 页面相关接口
 */
@RestController
@RequestMapping("/jxtc/enterprise/delivery/rider")
@RequiredArgsConstructor
@Slf4j
public class RiderLoginController {

    private final RiderLoginService riderLoginService;

    /**
     * 接口 6.1.1 配送员登录
     *
     * @param dto 手机号或账号 + 密码
     * @return 登录成功时 code 字段为 200，data 字段为 token 相关信息；新增失败时 code 字段为 400，message 字段返回错误提示信息
     */
    @PostMapping("/login")
    public Result<RiderLoginSuccessVO> login(@Valid @RequestBody RiderLoginDTO dto) {
        log.info("/login, request: {}", JSONObject.toJSONString(dto));
        RiderLoginSuccessVO vo = riderLoginService.login(dto.getPhone(), dto.getPassword());
        log.info("/login, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }
}
